package com.gmail.mosoft521.ch05.cms;

import java.util.HashMap;

/**
 * -Xmx200m -Xms200m -XX:+UseConcMarkSweepGC -Xloggc:gc.log -XX:+PrintGCDetails
 *
 * @author Administrator
 */
public class CMSTest {
    public static void main(String args[]) {
        MyThread t = new MyThread();
        PrintThread p = new PrintThread();
        t.start();
        p.start();
    }

    public static class MyThread extends Thread {
        HashMap map = new HashMap();

        @Override
        public void run() {
            try {
                while (true) {
                    byte[] b1;
                    if (map.size() * 512 / 1024 / 1024 >= 200) {
                        map.clear();

                        System.out.println("clean map2");
                    }
                    for (int i = 0; i < 1000; i++) {
                        b1 = new byte[512];
                        map.put(System.nanoTime(), b1);
                        for (int j = 0; j < 1000; j++) {
                            byte[] b2 = new byte[10];
                        }
                    }
                    Thread.sleep(1);
                }
            } catch (Exception e) {

            }
        }
    }

    public static class PrintThread extends Thread {
        public static final long starttime = System.currentTimeMillis();

        @Override
        public void run() {
            try {
                while (true) {
                    long t = System.currentTimeMillis() - starttime;
                    System.out.println(t / 1000 + "." + t % 1000);
                    Thread.sleep(100);
//					byte[] b=new byte[1024*1024*3];
                }
            } catch (Exception e) {

            }
        }
    }
}